const puppeteer = require('puppeteer')
const { PuppeteerExtra } = require('puppeteer-extra')
const { Cluster } = require('puppeteer-cluster')
const stealth = require('puppeteer-extra-plugin-stealth')()

const pptr = new PuppeteerExtra(puppeteer)

pptr.use(stealth)

export async function getPuppeteerCluster(options: any = {}) {
  const {
    maxConcurrency = 4,
    monitor = false,
    headless = true,
    devtools = false
  } = options

  const cluster = await Cluster.launch({
    puppeteer: pptr,
    concurrency: Cluster.CONCURRENCY_PAGE,
    maxConcurrency,
    monitor,
    puppeteerOptions: {
      headless,
      devtools,
      ignoreHTTPSErrors: true,
      args: [
        '--no-sandbox',
        '--disable-setuid-sandbox',
        '--disable-web-security'
      ]
    }
  })

  return cluster
}
